package com.zh.onlineexam.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.onlineexam.dto.TExamWrongRecordDto;
import com.zh.onlineexam.entity.User;
import com.zh.onlineexam.mapper.TExamWrongRecordMapper;
import com.zh.onlineexam.service.TExamWrongRecordService;
import com.zh.onlineexam.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @className WrongRecordController
 * @Description TODO
 * @Auther 29934
 * @Date 2022/9/4 9:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("/wrong")
@RequiredArgsConstructor
@Slf4j
public class WrongRecordController {

    private final TExamWrongRecordService wrongRecordService;
    private final UserService userService;

    private final TExamWrongRecordMapper wrongRecordMapper;

    /**
     *  获取本人登录用户名
     *  */
    public String getLoginUser(){
        Object loginId = StpUtil.getLoginId();
        User one = userService.getOne(new QueryWrapper<User>().eq("id", loginId));
        return one.getRealName();
    }

    /**
     * 查询本人错题本
     */
    @RequestMapping("/listAll")
    public List<TExamWrongRecordDto> getWrongRecordList(){
        return wrongRecordMapper.getWrongRecords(getLoginUser());
    }

    /**
     * 逻辑删除错题本
     */
    @RequestMapping("deleteById/{id}")
    public boolean  deleteById(@PathVariable Integer id){
        return wrongRecordService.removeById(id);
    }

}
